package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsCustomerFile;
import com.jy.wms.service.WmsCustomerFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsCustomerFileController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Wed Oct 11 11:26:05 CST 2017
 */
@RestController
@RequestMapping("/wmsCustomerFile")
@Api(value = "/wmsCustomerFile", description = "关于客户档案管理操作")
public class  WmsCustomerFileController extends BaseController<WmsCustomerFileService,WmsCustomerFile> {

    @ApiOperation(value = "客户档案管理 查询客户档案信息", response = JSONObject.class, notes = "查询客户档案信息")
    @PostMapping(value = "/list")
    public @ResponseBody
    JSONObject list(@RequestBody WmsCustomerFile entity, HttpSession session) {
        Page<WmsCustomerFile> list = new Page<WmsCustomerFile>();
        JSONObject json = getJSONObject();
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
        List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
        if(hz.size()>0){
            entity.setHzmc(hz.get(0).getShow_hzid());
            entity.setSshzid(hz.get(0).getHzid());
        }
        try {
            list = baseService.baseSelectPage(entity);
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
    }

    @PostMapping(value = "/getOwner")
    @ApiOperation(value = "客户档案管理 获取货主下拉列表选项", response = JSONObject.class,
            notes = "获取货主下拉列表选项 查询输入</br>{\"mc\":\"货主名称模糊查询\"}")
    public @ResponseBody
    JSONArray getOwner(HttpSession session, @RequestBody Map<Object, String> map) {
        List<Map<String, Object>> getMap = new ArrayList<Map<String, Object>>();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            map.put("ckid", String.valueOf(userinfo.getUser().getOrgId().get(0)));
            getMap = baseService.getOwner(map);

        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(getMap);
    }

    @PostMapping(value = "/baseSelectLine")
    @ApiOperation(value = "客户档案管理 获取路线下拉列表选项", response = JSONObject.class,
            notes = "获取路线下拉列表选项 查询输入</br>{\"xlmc\":\"路线名称模糊查询\"}")
    public @ResponseBody
    JSONArray baseSelectLine(HttpSession session, @RequestBody Map<Object, String> map) {
        List<Map<String, Object>> getMap = new ArrayList<Map<String, Object>>();
        try {
            String xlmc = map.get("xlmc");
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid = "";
            if (hz.size() > 0) {
                sshzid = hz.get(0).getHzid();
                getMap = baseService.baseSelectLine(xlmc, sshzid);
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(getMap);
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "客户档案管理 新增客户档案基本信息", response = JSONObject.class, notes = "新增客户档案基本信息")
    public @ResponseBody
    JSONObject save(HttpSession session, @RequestBody WmsCustomerFile entity) {
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
            }
            entity.setId(UUIDUtils.uuid());
            entity.setSfyx("1");
            entity.setSfjh("1");
            baseService.baseInsert(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增客户档案基本信息成功","", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增客户档案基本信息失败",e.getMessage(),"");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增客户档案基本信息成功","", JSONObject.fromObject(entity).toString());
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增客户档案基本信息失败",e.getMessage(),"");
        }
    }

    @PutMapping(value = "/edit")
    @ApiOperation(value = "客户档案管理 修改客户档案基本信息", response = JSONObject.class, notes = "修改客户档案基本信息")
    public @ResponseBody
    JSONObject edit(@RequestBody WmsCustomerFile entity) {
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改客户档案基本信息成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改客户档案基本信息失败",e.getMessage(),"");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改客户档案基本信息成功","", "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改客户档案基本信息失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "客户档案管理 查询唯一客户档案基本信息", response = JSONObject.class, notes = "查询唯一客户档案基本信息，'id'客户档案ID")
    @GetMapping(value = "/findOne/{id}")
    public @ResponseBody
    JSONObject getWmsOwnerById(@PathVariable("id") String id) {
        WmsCustomerFile wmsCustomerFile =new WmsCustomerFile();
        try {
            wmsCustomerFile = baseService.baseSelectByPrimaryKey(id);
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.fromObject(wmsCustomerFile);
    }

    @ApiOperation(value = "客户档案管理 作废客户档案", response = JSONObject.class, notes = "作废客户档案，'ids'客户档案ID")
    @PostMapping(value = "/cancel")
    public @ResponseBody
    JSONObject cancel(@RequestBody SetAndModifyModel setAndModifyModel) {
        try {
            baseService.cancelAll(setAndModifyModel.getIds());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"作废客户档成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"作废客户档失败",e.getMessage(),"");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"作废客户档成功","", "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"作废客户档失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "客户档案管理 激活客户档案", response = JSONObject.class, notes = "激活客户档案，'ids'客户档案ID")
    @PostMapping(value = "/activation")
    public @ResponseBody
    JSONObject activation(@RequestBody SetAndModifyModel setAndModifyModel) {
        try {
            List<String> ids = setAndModifyModel.getIds();
            WmsCustomerFile wmsCustomerFile = new WmsCustomerFile();
            for (String id : ids) {
                wmsCustomerFile.setId(id);
                wmsCustomerFile.setSfjh("1");
                baseService.baseUpdateByPrimaryKeySelective(wmsCustomerFile);
            }
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "激活客户档成功", "", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "激活客户档失败", e.getMessage(), "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "激活客户档成功", "", "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "激活客户档失败", e.getMessage(), "");
        }
    }

}
